<?php
class LoginFormClass {
	public $UserName;
	public $Mobile;
	public $Channel;
	public $Captcha;
	public $Message;
}
class loginAction extends AppAction {
	//URL地址：http://apple.zhaosheng.com/index.php/login
	public function index() {
		ViewData::$title = "用户信息查询";
		ViewData::$form_data = new LoginFormClass ();
		ViewData::$form_hint = new LoginFormClass ();
		if ($_POST) {
			$this->loginForm ();
			if (BaseAction::isObjectEmpty ( ViewData::$form_hint )) {
				try {
					$this->loginLogic ( ViewData::$form_data );
					Header ( "Location:  " . siteUrl ( ShowInfo ) . "" );
				} catch ( Exception $e ) {
					ViewData::$form_hint->Message = $e->getMessage ();
				}
			}
		}
		BaseAction::loadView ( 'login' );
	}
	
	//URL地址：http://apple.zhaosheng.com/index.php/login
	public function login() {
		if ($_POST) {
			try {
				$this->loginThrowForm ();
				$this->loginLogic ( ViewData::$form_data );
				Header ( "Location:  " . siteUrl ( ShowInfo ) . "" );
			} catch ( Exception $e ) {
				BaseAction::error ( $e->getMessage () );
			}
		} else {
			BaseAction::error ( "请不要非法操作" );
		}
	}
	
	//登出系统：http://apple.zhaosheng.com/index.php/login
	public function loginout() {
		UserSession::clearUserInfo ();
		Header ( "Location:  " . siteUrl ( ) . "" );
	}
	
	private function loginForm() {
		ViewData::$form_data = BaseAction::parseForm ( "POST", ViewData::$form_data );
		ViewData::$form_hint->UserName = BaseAction::checkForm ( 'UserName', 'POST', '姓名不能为空', array (2, 2, 64, "姓名长度错误" ), FALSE, TRUE );
		ViewData::$form_hint->Mobile = BaseAction::checkForm ( 'Mobile', 'POST', '手机号码不能为空', array (2, 11, 11, "手机号码长度错误" ), array ("isMobile", "手机号码格式有误" ), true );
		ViewData::$form_hint->Channel = BaseAction::checkForm ( 'Channel', 'POST', '查询位置不能为空', array (1, 1, 4, "查询位置不正确" ), FALSE, true );
		ViewData::$form_hint->Captcha = BaseAction::checkForm ( 'Captcha', 'POST', '验证码不能为空', array (2, 4, 4, "验证码长度错误" ), array ("isCharacter", "验证码格式有误" ), true );
		return;
	}
	private function loginThrowForm() {
		ViewData::$form_data = BaseAction::parseForm ( "POST", ViewData::$form_data );
		ViewData::$form_hint->UserName = BaseAction::checkForm ( 'UserName', 'POST', '姓名不能为空', array (2, 2, 64, "姓名长度错误" ), FALSE, FALSE );
		ViewData::$form_hint->Mobile = BaseAction::checkForm ( 'Mobile', 'POST', '手机号码不能为空', array (2, 11, 11, "手机号码长度错误" ), array ("isMobile", "手机号码格式有误" ), FALSE );
		ViewData::$form_hint->Channel = BaseAction::checkForm ( 'Channel', 'POST', '查询位置不能为空', array (1, 1, 4, "查询位置不正确" ), FALSE, FALSE );
		ViewData::$form_hint->Captcha = BaseAction::checkForm ( 'Captcha', 'POST', '验证码不能为空', array (2, 4, 4, "验证码长度错误" ), array ("isCharacter", "验证码格式有误" ), FALSE );
		return;
	}
	/**
	 * 
	 * @param LoginFormClass $info
	 * @throws Exception
	 */
	private function loginLogic($info) {
		if (! $this->checkCaptcha ( $info->Captcha )) {
			throw new Exception ( "验证码错误！" );
		}
		if ($info->Channel) {
			switch ($info->Channel) {
				case 3 :
					require_once APP_MODEL_PATH . 'e_full_time_student.php';
					$fullTimeMode = new FullTimeStudentModel ();
					$info = $fullTimeMode->getByLoginInfo ( $info->UserName, $info->Mobile );
					if ($info) {
						$info->Channel = 3;
						UserSession::setUserInfo ( $info );
						return true;
					}
					break;
				case 4 :
					require_once APP_MODEL_PATH . 'e_lotus_student.php';
					$lotusMode = new LotusStudentModel ();
					$info = $lotusMode->getByLoginInfo ( $info->UserName, $info->Mobile );
					if ($info) {
						$info->Channel = 4;
						UserSession::setUserInfo ( $info );
						return true;
					}
					break;
				case 1 :
					require_once APP_LIB_PATH . 'campLib.php';
					$lib = new CampLib ();
					$info = $lib->isExist ( $info->UserName, $info->Mobile );
					if ($info) {
						$info->Channel = 1;
						UserSession::setUserInfo ( $info );
						return true;
					}
					break;
				default :
					throw new Exception ( "查询位置选择错误" );
					break;
			}
		}
		throw new Exception ( "用户名密码不正确" );
	}
	
	/**
	 * 显示验证码
	 * @return void
	 */
	public function showCaptcha() {
		require_once SOURCE_EXT_PATH . 'library/CaptchaLib.php';
		$code = CaptchaLib::getCode ();
		UserSession::setCaptcha ( $code );
		CaptchaLib::showImage ( $code );
	}
	
	/**
	 * 检查验证码
	 * @param string $inputCaptch
	 * @return string
	 */
	public function checkCaptcha($inputCaptch) {
		
		$code = UserSession::getCaptcha ();
		//UserSession::clearCaptcha ();
		return strtolower ( $inputCaptch ) == strtolower ( $code );
	}
}
